package com.tomtop.config;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.tomtop._const.Const;
import com.tomtop.core.Context;
import com.tomtop.core.utils.JSONUtils;

@Component
@ConfigurationProperties(prefix="spring.application")
public class WebSiteConfig implements InitializingBean {
	private static Logger log = LoggerFactory .getLogger(WebSiteConfig.class);

	@Override
	public void afterPropertiesSet() throws Exception {
		log.info("当前执行的程序是 : "+WebSiteConfig.getAppName()+" "+WebSiteConfig.getVersion());
		log.info("当前站点静态资源版本号 :  "+WebSiteConfig.getResourceVersion());
		log.info("默认选中的站点:  "+WebSiteListConfig.getDefault());
		log.info("站点配置列表 :  "+JSONUtils.toJSONString(WebSiteListConfig.getSiteList()));
	}
	private static String version="0.0.0",home="/",appname="";
	private static String resourceVersion="";
	public static String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		WebSiteConfig.version = version;
	}
	public static String getResourceVersion() {
		return resourceVersion;
	}
	public void setResourceVersion(String resourceVersion) {
		WebSiteConfig.resourceVersion = resourceVersion;
	}
	public static String getHome() {
		return home;
	};
	public void setHome(String home) {
		WebSiteConfig.home = home;
	}
	public static String getAppName() {
		return appname;
	}
	public void setName(String appname) {
		WebSiteConfig.appname = appname;
	}
	public static String getPrefix() {
		if(Context.isPc())
			return getWebSite().getPrefix().getPc();
		return getWebSite().getPrefix().getM();
	}
	public static boolean isContain(){
		String hostname = Context.getHostName();
		return hostname==null||!WebSiteListConfig.getSiteList().containsKey(hostname);
	}
	public static String getHostName() {
		if(isContain()){
			return Context.getHostName();
		}
		log.warn("请注意站点不存在,将使用默认站点!  {} ",Context.getContext());
		return WebSiteListConfig.getDefault();
	}

	public static String getLoginBreak(String backUrl) {
		String url;
		if(Context.isPc())
			url= WebSiteConfig.getWebSite().getUrl("login-and-break");
		else
			url= WebSiteConfig.getWebSite().getUrl("m-login-and-break");
		if(url==null)
			return "";
		if(backUrl==null)
			backUrl="";
		else
			try {
				backUrl=URLEncoder.encode(backUrl, Const.DEFAULT_CHARSET.name());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		return url.replace("$backUrl", backUrl);
	}
	
	public static String getVhost() {
		return getPrefix()+"." +  getWebSite().getHost();
	}
	
	public static String getShopHome() {
		if(Context.isApp())
			return WebSiteConfig.getWebSite().getUrl("app-gohome");
		if(Context.isPc())
			return WebSiteConfig.getWebSite().getUrl("shop-home");
		return WebSiteConfig.getWebSite().getUrl("m-shop-home");
	}

	public static String getCartHome() {
		if(Context.isApp())
			return WebSiteConfig.getWebSite().getUrl("app-gocart");
		return "//"+Context.getHost();
	}
	
	public static String getStaticResourcePrefix() {
		 if(!Context.isPc())
			return WebSiteConfig.getWebSite().getUrl("m-cdn-url");
		return WebSiteConfig.getWebSite().getUrl("cdn-url");
	}
	
	public static String getCommonJsPrefix() {
		 if(!Context.isPc())
			return WebSiteConfig.getWebSite().getUrl("m-common-cdn-url");
		return WebSiteConfig.getWebSite().getUrl("common-cdn-url");
	}
	
	public static WebSite getWebSite() {
		String siteName=getHostName();
		return WebSiteListConfig.getSiteList().get(siteName);
	}
	public static WebSite getDefaultWebSite() {
		String siteName=WebSiteListConfig.getDefault();
		return WebSiteListConfig.getSiteList().get(siteName);
	}
	
	@ConfigurationProperties(prefix="website")
	@Configuration
	public static class WebSiteListConfig{
		private static String _default;
		private static Map<String,WebSite> sites;
		
		public static String getDefault() {
			return _default;
		}

		public void setDefault(String _default) {
			WebSiteListConfig._default = _default;
		}
		private static Map<String,WebSite> getSiteList() {
			return sites;
		}
		public   Map<String, WebSite> getSites() {
			return sites;
		}
		public void setSites(Map<String, WebSite> sites) {
			log.debug("初始化站点数据开始");
			WebSiteListConfig.sites = sites;
		}
		
	}
	
	public static class CustomerService{
		private String email,href;

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getHref() {
			return href;
		}

		public void setHref(String href) {
			this.href = href;
		}
	}
	public static class Prefix{
		private String m , pc , my , cdn;
		
		public String getCdn() {
			return cdn;
		}

		public void setCdn(String cdn) {
			this.cdn = cdn;
		}

		public String getMy() {
			return my;
		}

		public void setMy(String my) {
			this.my = my;
		}

		public String getM() {
			return m;
		}

		public void setM(String m) {
			this.m = m;
		}

		public String getPc() {
			return pc;
		}

		public void setPc(String pc) {
			this.pc = pc;
		}
	}
	public static class WebSite{
	    private int id;
		private CustomerService service;
		private String ico,imgUrlPrefix,host;
		private Map<String,String> url;
		private Prefix prefix;
		private String defaultLanguage;
		private Map<String,Object> properties;
	    public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getHost() {
			return host;
		}
		public void setHost(String host) {
			this.host = host;
		}
		
		public Prefix getPrefix() {
			return prefix;
		}
		public void setPrefix(Prefix prefix) {
			this.prefix = prefix;
		}
		public CustomerService getService() {
			return service;
		}
		public void setService(CustomerService service) {
			this.service = service;
		}
		public String getImgUrlPrefix() {
			return imgUrlPrefix;
		}
		public void setImgUrlPrefix(String imgUrlPrefix) {
			this.imgUrlPrefix = imgUrlPrefix;
		}
		public Map<String, Object> getProperties() {
			return properties;
		}
		public void setProperties(Map<String, Object> properties) {
			this.properties = properties;
		}
		public String getIco() {
			return ico;
		}
		public void setIco(String ico) {
			this.ico = ico;
		}
		public Map<String, String> getUrl() {
			return url;
		}
		public String getUrl(String key) {
			String url = this.url.get(key);
			return url == null ? "" : url;
		}
		public void setUrl(Map<String, String> url) {
			this.url = url;
		}
		public String getDefaultLanguage() {
			return defaultLanguage == null ? Const.DEFAULT_LANGUAGE : defaultLanguage;
		}
		public void setDefaultLanguage(String defaultLanguage) {
			this.defaultLanguage = defaultLanguage;
		}
	}
}
